<?php
declare (strict_types=1);

namespace sws;


class Console
{

    const INFO = 0;
    const SUCCESS = 1;
    const DEBUG = 2;
    const WARNING = 3;
    const ERROR = 4;


    /**
     * 获取实例
     * @return Console
     */
    public static function getInstance(): Console
    {
        return Container::getInstance()->make(Console::class);
    }

    /**
     * 向终端换行输出
     * @param string $content
     * @param int $level
     */
    public function writeln(string $content, int $level = self::INFO)
    {
        switch ($level) {
            case self::SUCCESS:
                $content = sprintf("\e[0;32m%s\e[0m", $content);
                break;
            case self::DEBUG:
                $content = sprintf("\e[0;36m%s\e[0m", $content);
                break;
            case self::WARNING:
                $content = sprintf("\e[0;33m%s\e[0m", $content);
                break;
            case self::ERROR:
                $content = sprintf("\e[41;37m%s\e[0m", $content);
                break;
        }
        $content .= PHP_EOL;
        $output = @fopen('php://output', 'w');

        if (false === @fwrite($output, $content)) {
            echo $content;
        } else {
            fflush($output);
        }
    }

    /**
     * 向终端输出框架名
     */
    public function writeSWS()
    {
        $sws = <<<EOL
                            .---.                    
      .--.--.              /. ./|     .--.--.        
     /  /    '          .-'-. ' |    /  /    '       
    |  :  /`./         /___/ \: |   |  :  /`./       
    |  :  ;_        .-'.. '   ' .   |  :  ;_         
     \  \    `.    /___/ \:     '    \  \    `.      
      `----.   \   .   \  ' .\        `----.   \     
     /  /`--'  /    \   \   ' \ |    /  /`--'  /     
    '--'.     /      \   \  |--"    '--'.     /      
      `--'---'        \   \ |         `--'---'       
                       '---"                                                                                                             
EOL;
        $this->writeln($sws);
    }

    /**
     * 检查command
     * @return array|void
     */
    public function getCommand()
    {
        global $argv;

        if (count($argv) <= 2) {
            $this->writeSWS();
            $this->writeln($this->help());
            die;
        }
        $serverName = $argv[1];
        $option = $argv[2];
        $daemonize = isset($argv[3]) && $argv[3] == '-d';
        //获取配置文件所有server
        $servers = Config::getInstance()->get('server.servers');
        $serverNames = array_keys($servers);
        if (!in_array($serverName, $serverNames)) {
            $this->writeln("Non-existent service:{$serverName}, Check the config file .", self::ERROR);
            die;
        }
        if (!in_array($option, ['start', 'stop', 'restart', 'reload'])) {
            $this->writeln("Invalid Command:{$option}, Expected start|stop|restart|reload .", self::ERROR);
            die;
        }
        return [$serverName, $option, $daemonize];
    }

    /**
     * 获取命令帮助
     * @return string
     */
    public function help(): string
    {
        $version = App::getInstance()->getVersion();
        return <<<EOL

version $version
Usage:
    serverName options [arguments]
    
ServerName: Server name, to server config file.
    
Options:
    start         Start Service.
    stop          Stop Service.
    restart       Restart Service.
    reload        Hot Restart Service.
    
arguments:
    -d            Enable Daemon.
    
EOL;
    }


}